package com.jiming.webrpc;

import com.jiming.webrpc.session.Session;
import com.jiming.webrpc.utiles.ThreadUtile;

/**
 * 远程主机
 * @author straw
 */
public class RemoteHost extends Host {
	
	private LocalHost localHost = null;
	
	private String token = null;
	
	private long activeTime = 0;
	
	private Thread flush_work = null;
	
	private Session session = null;
	
	private RemoteHost() {
		//开启异步刷新远程主机任务
		syncFlush();
	}
	
	/**
	 * 异步刷新远程主机基本信息（活跃时间、ip、端口）如果有变化可以及时更新
	 */
	private void syncFlush() {
		flush_work = new Thread(() ->{
			while(true) {
				try {
					if(this.token != null) {
						Customer customer = this.localHost.getCustomerByToken(this.token);
						if(customer != null) {
							this.setActiveTime(customer.getActiveTime());
							this.setLocalPort(customer.getLocalPort());
							this.setNatIp(customer.getNatIp());
							this.setNatPort(customer.getNatPort());
						}
					}
				}catch (Exception e) {
				}
				
				//3秒刷新一次
				ThreadUtile.sleep(3 * 1000);
			}
		});
		flush_work.start();
	}
	
	/**
	 * 关闭远程主机
	 */
	public synchronized void close() {
		System.out.println("关闭远程主机...");
		if(this.flush_work != null) {
			this.flush_work.interrupt();
			this.flush_work = null;
		}
		
		//关闭会话
		if(session != null) {
			session.close();
			session = null;
		}
	}
	
	public String getToken() {
		return token;
	}


	public LocalHost getLocalHost() {
		return localHost;
	}

	public void setLocalHost(LocalHost localHost) {
		this.localHost = localHost;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public long getActiveTime() {
		return activeTime;
	}


	public void setActiveTime(long activeTime) {
		this.activeTime = activeTime;
	}


	public boolean isOnline() {
		if((System.currentTimeMillis() - this.activeTime) > (10 * 1000)) {
			//超过10秒没有心跳
			return false;
		}else {
			return true;
		}
	}
	
	
	public static RemoteHost newBuild(LocalHost localHost) {
		RemoteHost remoteHost = new RemoteHost();
		remoteHost.setLocalHost(localHost);
		return remoteHost;
	}

	/**
	 * 和远程主机B建立连接会话
	 * @return Session
	 */
	public synchronized Session openSession() {
		if(session != null && session.isOpen()) {
			//已经打开了
			return this.session;
		}
		
		//一个远程主机只有一个会话
		if(this.session == null) {
			this.session = Session.newBuile(this);
		}else {
			this.session.close();
		}
		
		if(this.isOnline()) {
			this.session.start();
		}
		
		return this.session;
	}

	/**
	 * 与A建立susession会话
	 */
	public synchronized SessionStatus accptSessionA(String customerASessionId) {
		if(session != null && session.isOpen()) {
			//已经打开了
			return session.getStatus();
		}
		
		if(session != null && session.isStarting()) {
			//正在打开
			return session.getStatus();
		}
		
		//一个远程主机只有一个会话
		if(this.session == null) {
			this.session = Session.newBuile(this);
		}else {
			this.session.close();
		}
		
		if(this.isOnline()) {
			new Thread(()->{
				this.session.agreeConnection();
			}).start();
			
			//已经打开中
			return SessionStatus.starting;
		}else {
			//已经关闭
			return SessionStatus.closed;
		}
	}

	/**
	 * 获取当前会话的sessionId
	 * @return
	 */
	public String getSessionId() {
		if(this.session != null) {
			return this.session.getId();
		}
		return null;
	}
	
	
}
